<?php
class isp_lap_client_invoice_berlangganan_model extends MY_Model {
	var $model = 'isp_lap_client_invoice_berlangganan';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT
					a.id_invoice,
					a.input_ppn,
					a.kode_kontrak,
					a.kode_customer,
					a.nama_customer,
					a.abodemen,
					a.total,
					a.ppn,
					a.no_invoice,
					TO_CHAR(a.tanggal_invoice,'DD/MM/YYYY') as tanggal_invoice,
					a.status,
					TO_CHAR(a.tanggal_bayar,'DD/MM/YYYY') as tanggal_bayar,
					a.periode_tagihan,
					a.keterangan,
					a.biaya_pendaftaran,
					a.terbilang,
					TO_CHAR(a.due_date,'DD/MM/YYYY') as due_date,
					a.nama_jenis_barang,
					a.bandwidth,
					b.no_id
					FROM	ISP_CLIENT_INVOICE a
					INNER JOIN ISP_CLIENT_KONTRAK b ON a.kode_kontrak = b.kode_kontrak
					WHERE	(UPPER(a.no_invoice) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.kode_kontrak) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'a.no_invoice' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
	
	function GetJSON_detail($id)
	{
		$ls_sql	= "	SELECT *
					FROM isp_custom_invoice_detail
					WHERE no_invoice = '$id'
					AND (UPPER(id_custom_detail) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(no_invoice) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'id_custom_detail' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}


	function GetCetak($as_no_invoice)
	{
		$result1 = $this->db->query("   SELECT  a.id_invoice,a.input_ppn,a.kode_kontrak,a.kode_customer,a.no_invoice,
												a.abodemen,a.ppn,b.kota,b.kode_pos,
												TO_CHAR(a.tanggal_invoice,'DD/MM/YYYY') as tanggal_invoice,
												TO_CHAR(a.tanggal_bayar,'DD/MM/YYYY') as tanggal_bayar, 
												a.keterangan,a.terbilang,a.periode_tagihan,
												TO_CHAR(a.due_date,'DD/MM/YYYY') as due_date,
												a.nama_jenis_barang,a.bandwidth,a.total,a.biaya_pendaftaran,
												b.nama,b.nama_perusahaan,b.alamat,c.no_id
										FROM ISP_CLIENT_INVOICE a
										LEFT JOIN ISP_CUSTOMER b ON a.kode_customer = b.kode_customer
										JOIN ISP_CLIENT_KONTRAK c ON a.kode_kontrak = c.kode_kontrak
					                    WHERE a.NO_INVOICE ='".$as_no_invoice."' ");

		
		return $result1->row_array();
		
	}

	function GetCetakCustom($as_no_invoice)
	{
		$result1 = $this->db->query(" SELECT harga,uraian
									  FROM isp_custom_invoice_detail
                    				  WHERE no_invoice='$as_no_invoice'");

		
		return $result1->result_array();
		
	}

	function GetRowCountCustom()
	{
		$result1 = $this->db->query(" SELECT COUNT(1) 
										FROM ISP_CLIENT_INVOICE a
										LEFT JOIN ISP_CUSTOMER b ON a.kode_customer = b.kode_customer
										LEFT JOIN isp_custom_invoice_detail c ON a.no_invoice = c.no_invoice");

		
		return $result1->result_array();
		
	}

	function Add($arr_data){
		$this->db->insert('isp_custom_invoice_detail', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('isp_custom_invoice_detail', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_kode_kontrak){
		$result = $this->db->get_where('ISP_CLIENT_INVOICE', array('kode_kontrak' => $as_kode_kontrak));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_kode_kontrak)
	{
		$result = $this->db->get_where('ISP_CLIENT_INVOICE', array('kode_kontrak' => $as_kode_kontrak));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
		console.log($result);
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM isp_custom_invoice_detail WHERE id_custom_detail = '".$arr_rule['id_custom_detail']."'"))<=0)
			{
				return $this->error_delete.' [id: '.$arr_rule['id_custom_detail']."]<br />";
			}else{
				$this->db->delete('isp_custom_invoice_detail', array('id_custom_detail' => $arr_rule['id_custom_detail']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

}
?>